﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Trang;
using Microsoft.Win32;

namespace ArchSoft
{
    public class CodeNameUtilities
    {
        public const char WindowNameSeparator = '_';
        public const char WindowDrawingSeparator = '-';
        public const char DrawingNameSeparator = '-';

        public static string GetCodeByName(string name)
        {
            return name.Split('_')[0];
        }

       

        public static string ToNameWithoutEnxtension(string name)
        {
            string fileName = Path.GetFileNameWithoutExtension(name);
            if (fileName.IndexOf("_")>1)
            {
                return fileName.Split('_')[1];
            }
            return fileName;
        }

        public static string ToCodeNameWithoutEnxtension(string name)
        {
            return Path.GetFileNameWithoutExtension(name);
        }


        internal static string GetWindowCodeFromFullName(string windowName)
        {
            if (windowName.IndexOf(CodeNameUtilities.WindowNameSeparator)>=0)
            {
                return windowName.Split(new char[] { CodeNameUtilities.WindowNameSeparator })[0];
            }
            return string.Empty;
        }

        internal static string GetWindowNameFromFullName(string windowName)
        {
            if (windowName.IndexOf(CodeNameUtilities.WindowNameSeparator)>=0)
            {
                return windowName.Split(new char[] { CodeNameUtilities.WindowNameSeparator })[1]; 
            }
            return string.Empty;
        }

        internal static string BuildWindowName(string code, string name)
        {
            return string.Concat(new string[] {code,CodeNameUtilities.WindowNameSeparator.ToString(),name});
        }

        internal static string GenerateDrawingName(int windowPos, int filePos, string fileName)
        {
            if (windowPos == -1)
            {
                if (fileName == "*")
                    fileName = string.Format("{1:00}_{2}" + Constants.DWG_EXT_FILE, windowPos, filePos, fileName);
                else
                {
                    if (fileName.StartsWith( ProjectManager.PROJ_DEFAULT_CHAR + "_"))
                        fileName = fileName.Substring(2);
                    fileName = ProjectManager.PROJ_DEFAULT_CHAR + "_" + fileName + Constants.DWG_EXT_FILE;
                }
            }
            else
            {
                if (fileName.StartsWith(ProjectManager.PROJ_DEFAULT_CHAR + "_"))
                    fileName = fileName.Substring(2);
                fileName = string.Format("A{0}-{1:00}_{2}" + Constants.DWG_EXT_FILE, windowPos, filePos, fileName);
            }
            return fileName;
        }

        internal static string GenerateDrawingName(int windowPos, int filePos, string fileName, string ParentFolder)
        {
            if (windowPos == -1)
            {               
                fileName = string.Format("{1:00}_{2}" + Constants.DWG_EXT_FILE, windowPos, filePos, fileName);
            }
            else
                fileName = string.Format("A{0}-{1:00}_{2}" + Constants.DWG_EXT_FILE, windowPos, filePos, fileName);
            return fileName;
        }

        internal static string ReplaceWindowName(string oldFullName, string newName)
        {
            return BuildWindowName(GetWindowCodeFromFullName(oldFullName), newName);
        }

        internal static int GetOrderInWindow(string name)
        {
            string code = GetCodeByName(name);
            if (code.Contains(WindowDrawingSeparator.ToString()))
            {
                return Int32.Parse(code.Split(new char[] { WindowDrawingSeparator })[1]);
            }
            return -1;//Int32.Parse( code);
        }

        internal static string ReplaceDrawingName(string fullName, string newNewWithoutExtension)
        {
            return fullName.Replace(ToNameWithoutEnxtension(fullName), newNewWithoutExtension);
        }

        internal static string ToOutputNameInStorageFile(string p)
        {
            return "Serial " + GetWindowCodeFromFullName(p) + " " + GetWindowNameFromFullName(p); ;
        }

        /// <summary>
        /// Ham nay dung để get full path acad.exe(AutoCad)
        /// </summary>
        /// <returns></returns>
        public static string GetAutocadPath()
        {
            string Software = string.Empty;
            string findSoft = "AutoCAD";

            //The registry key:
            string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
            {
                foreach (string skName in rk.GetSubKeyNames())
                {
                    if (skName.ToLower().Contains(findSoft.ToLower()))
                    {
                        using (RegistryKey sk = rk.OpenSubKey(skName))
                        {
                            Software = sk.GetValue("InstallLocation").ToString();
                        }
                        break;
                    }
                }
            }
            string fullPath = Path.Combine(Software, "acad.exe");
            
            //truong hop ko nhan dc ACad = cach nay thi chuyen cach 2
            if (fullPath=="acad.exe")
            {
                RegistryKey cadKey = Registry.LocalMachine.OpenSubKey(CADConstants.CAD_REGISTRY_KEY);
                string key = cadKey.GetSubKeyNames()[0];//R17.0 .....
                RegistryKey subKey = cadKey.OpenSubKey(key);
                subKey = subKey.OpenSubKey(subKey.GetSubKeyNames()[0]);
                if (subKey != null)
                    fullPath = (string)subKey.GetValue(CADConstants.CAD_LOCATION_REGISTRY_VALUE_NAME) + System.IO.Path.DirectorySeparatorChar + CADConstants.AUTOCAD_EXECUTE_FILENAME; 
            }
            return fullPath;
        }

        public static string GetDateYYMMDD(DateTime date)
        {
            StringBuilder s = new StringBuilder();
            s.Append(date.Year.ToString().Substring(2));
            s.Append(date.Month.ToString("00"));
            s.Append(date.Day.ToString("00"));
            return s.ToString();
        }
    }
}
